package cn.iocoder.yudao.module.vote.controller.admin.voteuserresult;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.vote.aop.SubmitCheck;
import cn.iocoder.yudao.module.vote.controller.admin.voteinfo.vo.VoteInfoPageReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteinfo.vo.VoteInfoRespVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteOnlineUpdateReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteUserResultExportReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteUserResultPageReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteUserResultRespVO;
import cn.iocoder.yudao.module.vote.convert.voteinfo.VoteInfoConvert;
import cn.iocoder.yudao.module.vote.convert.voteuserresult.VoteUserResultConvert;
import cn.iocoder.yudao.module.vote.dal.dataobject.userref.VoteUserRefDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteinfo.VoteInfoDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresult.VoteUserResultDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresultdetail.VoteUserResultDetailDO;
import cn.iocoder.yudao.module.vote.service.userref.VoteUserRefService;
import cn.iocoder.yudao.module.vote.service.voteinfo.VoteInfoService;
import cn.iocoder.yudao.module.vote.service.voterecord.VoteRecordService;
import cn.iocoder.yudao.module.vote.service.voteuserresult.VoteUserResultService;
import cn.iocoder.yudao.module.vote.service.voteuserresultdetail.VoteUserResultDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 投票结果")
@RestController
@RequestMapping("/vote/online")
@Validated
public class VoteOnlineController {

    @Resource
    private VoteUserResultService userResultService;
    @Resource
    private VoteInfoService voteInfoService;
    @Resource
    private VoteRecordService voteRecordService;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private VoteUserResultDetailService resultDetailService;

    @Resource
    private VoteUserRefService voteUserRefService;




    @SubmitCheck(value="updateOnlineResult",expireSeconds=10)
    @PostMapping("/updateH5Result")
    @ApiOperation("更新投票结果")
    public CommonResult<Boolean> updateH5Result(@Valid @RequestBody VoteOnlineUpdateReqVO onlineUpdateReqVO, HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request, null);
        onlineUpdateReqVO.setIp(ip);
        userResultService.updateOnlineResult(onlineUpdateReqVO);
        return success(true);
    }



    @SubmitCheck(value="updateOnlineResult",expireSeconds=10)
    @PostMapping("/update")
    @ApiOperation("更新投票结果")
    @PreAuthorize("@ss.hasPermission('vote:online:update')")
    public CommonResult<Boolean> updateOnlineResult(@Valid @RequestBody VoteOnlineUpdateReqVO onlineUpdateReqVO, HttpServletRequest request) {
        AdminUserRespDTO respDTO = adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());
        onlineUpdateReqVO.setIp(respDTO.getNickname());
        userResultService.updateOnlineResult(onlineUpdateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得投票结果")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('vote:online:query')")
    public CommonResult<VoteUserResultRespVO> getUserResult(@RequestParam("id") Integer id) {
        VoteUserResultDO userResult = userResultService.getUserResult(id);
        VoteUserResultRespVO resultRespVO = VoteUserResultConvert.INSTANCE.convert(userResult);

        VoteUserResultDetailDO detailDO=resultDetailService.selectByResultId(userResult.getId().longValue());
        if(detailDO!=null) {
            resultRespVO.setAppraiseUrl(detailDO.getAppraiseUrl());
            resultRespVO.setAssessUrl(detailDO.getAssessUrl());
            resultRespVO.setEvidenceUrl(detailDO.getEvidenceUrl());
            resultRespVO.setInfoUrl(detailDO.getInfoUrl());
            resultRespVO.setRecommendUrl(detailDO.getRecommendUrl());
        }
        return success(resultRespVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得投票结果列表")
    @PreAuthorize("@ss.hasPermission('vote:online:query')")
    public CommonResult<List<VoteUserResultRespVO>> getUserResultList(VoteUserResultExportReqVO exportReqVO) {
        AdminUserRespDTO adminUserRespDTO = adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());

        VoteInfoDO voteInfoDO = voteInfoService.getInfo(exportReqVO.getVoteId());

        VoteUserRefDO ref =  voteUserRefService.getByUserIdAndVoteId(exportReqVO.getVoteId(),adminUserRespDTO.getId());
        exportReqVO.setGroupName(ref.getGroupName());
        exportReqVO.setIsQuote(voteInfoDO.getIsQuote());
        List<VoteUserResultDO> list = userResultService.getUserResultList(exportReqVO);

        List<VoteUserResultRespVO> respVOList =  VoteUserResultConvert.INSTANCE.convertList(list);


        return success(respVOList);
    }




    @GetMapping("/vote-page")
    @ApiOperation("获得投票规则分页")
    @PreAuthorize("@ss.hasPermission('vote:online:query')")
    public CommonResult<PageResult<VoteInfoRespVO>> getInfoPage(@Valid VoteInfoPageReqVO pageVO) {
        PageResult<VoteInfoRespVO> pageResult = VoteInfoConvert.INSTANCE.convertPage(voteInfoService.getInfoPage(pageVO));
        if(CollUtil.isNotEmpty(pageResult.getList())){
            for(VoteInfoRespVO vote :pageResult.getList()){
                vote.setIsVote(voteRecordService.isVote(vote.getId()));
            }
        }

        return success(pageResult);
    }


    @GetMapping("/page")
    @ApiOperation("获得投票结果分页")
    @PreAuthorize("@ss.hasPermission('vote:online:query')")
    public CommonResult<PageResult<VoteUserResultRespVO>> getUserResultPage(@Valid VoteUserResultPageReqVO pageVO) {
        PageResult<VoteUserResultDO> pageResult = userResultService.getUserResultPage(pageVO);

//        for(VoteUserResultDO result:pageResult.getList()){
//            VoteStatusUtils.setVoteStatus(result);
//        }
        return success(VoteUserResultConvert.INSTANCE.convertPage(pageResult));
    }

//    @GetMapping("/group-name")
//    @ApiOperation("获取组信息")
//    @PreAuthorize("@ss.hasPermission('vote:online:query')")
//    public CommonResult<List<VoteUserResultGroupVO>> getGroupName(@RequestParam("voteId")Integer voteId) throws IOException {
//        AdminUserRespDTO adminUserRespDTO = adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());
//        VoteUserRefDO ref =  voteUserRefService.getByUserIdAndVoteId(voteId,adminUserRespDTO.getId());
//        return success(userResultService.groupByGroupName(voteId,ref.getGroupName()));
//    }


    @GetMapping("/userIsVote")
    @ApiOperation("是否已投票")
    @PreAuthorize("@ss.hasPermission('vote:online:query')")
    public CommonResult<Boolean> userIsVote(@RequestParam("voteId")Integer voteId) {
        return success(voteRecordService.isVote(voteId));
    }


    @GetMapping("/ipIsVote")
    @ApiOperation("ip是否已投票")
    @PreAuthorize("@ss.hasPermission('vote:online:query')")
    public CommonResult<Boolean> ipIsVote(@RequestParam("voteId")Integer voteId,HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request, null);
        return success(voteRecordService.ipIsVote(voteId,ip));
    }

}
